from datetime import date
import re
import requests


def get_img_url():
    url = "https://www.zgdypw.cn/"
    req = requests.get(url)
    req= req.content.decode("utf-8")
    obj = re.search('<li><a href="(?P<url>.*?)" target="_blank">全国电影票房周报',req)
    result = re.compile('<li><a href="(?P<url>.*?)" target="_blank">全国电影票房周报', re.S)
    ret = result.finditer(obj.group())
    for it in ret:
        return url+it.group("url")[1:]
def getimgurl(url):
    req = requests.get(url)
    req = req.content.decode("utf-8")
    obj = re.compile('border-top-width: 0px" alt="" oldsrc="(?P<url>.*?)" src="./',re.S)
    ret = obj.finditer(req)
    index=1
    for it in ret:
        if index==2:
            return it.group("url")
        index=index+1
def downloadimg():
    data=str(date.today()).split("-")
    req=requests.get("https://www.zgdypw.cn//sc/sjbg/"+data[0]+data[1]+"/"+data[2]+"/"+getimgurl(get_img_url()))
    if req.status_code != 200:
        print('下载异常')
        return
    try:
        with open("1.jpg", 'wb') as f:
            # req.content为获取html的内容
            f.write(req.content)
            print('下载成功')
    except Exception as e:
        print(e)


if __name__ == '__main__':
    downloadimg()